/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministradorSucursal.java
 *
 * Created on 19-nov-2009, 22:18:54
 */

package scapi.gui;

import java.util.logging.Level;
import java.util.logging.Logger;
import scapi.utilerias.ModeloTablaSucursal;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.sql.SQLException;
import javax.swing.ListSelectionModel;
import scapi.entidades.Sucursal;
import scapi.servidor.servidores.ServidorASucursal;
import scapi.utilerias.ServidorFactory;
import java.io.*;
import java.lang.*;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import scapi.entidades.Direccion;
import scapi.utilerias.EValidacion;
import scapi.utilerias.ExcepcionValidacion;


/**
 *
 * @author Emmanuel
 */
public class AdministradorAdministrarSucursal extends javax.swing.JPanel {
    //private final ServidorASucursal servSuc;
    private int numRegistros;
    private  ModeloTablaSucursal modeloTable;
    ModeloTablaSucursal modelo;
    Direccion direccionSucu,direccion2;
    Sucursal sucu,sucursal;
    int idSuc,numeroExt,codPostal,horaIn,horaFin,indice;
    String calle, numeroInt,colonia,delegacion,estado,openHorario,closeHorario,elimSucu, dele;


    private final ServidorASucursal servSuc2;

    /** Creates new form AdministradorSucursal */
    public AdministradorAdministrarSucursal() throws RemoteException, NotBoundException, MalformedURLException, SQLException {
    modeloTable= new ModeloTablaSucursal();
        initComponents();

        txtPais.setText("Mexico");
        txtPais.setEnabled(false);
        txtId.setEnabled(false);
        jButton5.setEnabled(false);

        //filtro oculto

        jLabel10.setVisible(false);
        jTextField9.setVisible(false);
        jButton4.setVisible(false);

     desactivarFormulario();

    //servSuc = (ServidorASucursal) ServidorFactory.obtenerServidor(ServidorFactory.ServidorSucursal);
    servSuc2 =  (ServidorASucursal) ServidorFactory.obtenerServidor(ServidorFactory.ServidorSucursal);

    jTable1.setModel(new DefaultTableModel(servSuc2.consultaGeneral(),
        new String [] {"ID", "Calle", "Numero", "Colonia", "C.P.","Delegacion","Estado", "Pais","Abre", "Cierra" }
                ));
    //System.out.println(servSuc2.ultimoUsuario());

    modelo = new ModeloTablaSucursal();

    txtId.setText(servSuc2.ultimoUsuario()+1+"");

    //Evento doble click tabla
    jTable1.addMouseListener(new MouseAdapter(){
        
        public void mouseClicked(MouseEvent e){
        if (e.getClickCount() == 2){
            idSuc = Integer.parseInt((String)modeloTable.getValueAt(jTable1.getSelectedRow(),0));
                    try {
                        Sucursal nuevaSuc = servSuc2.consultaSucursal(idSuc);
                        Direccion nuevaDir = nuevaSuc.getDir();

                      //  llena los campos con los datos de sucursal seleccionada
                        txtId.setText(Integer.toString(idSuc));
                        txtNumero.setText(nuevaDir.getNumInt());
                        txtNumero.setText(Integer.toString(nuevaDir.getNumExt()));
                        txtColonia.setText(nuevaDir.getColonia());
                        txtCalle.setText(nuevaDir.getCalle());
                        txtDeleg.setText(nuevaDir.getDelegacion());
                        txtCodPostal.setText(Integer.toString(nuevaDir.getCodPostal()));
                        
                       indice = obtenerIndex(nuevaDir.getEstado());
                       // 88888888888888888888888888
                       comEst.setSelectedIndex(indice);

                        txtPais.setText(nuevaDir.getPais());

                        String horaopen = nuevaSuc.getOpen();
                        //int puntos = horaopen.charAt(':');
                        System.out.print(horaopen);
                        String horaInicio = horaopen.substring(0, 2);
                        String minutosInicio = horaopen.substring(3);



                        String horafin = nuevaSuc.getClose();
                        //int puntoFin = horafin.charAt(':');
                        String horaFin = horafin.substring(0, 2);
                        String miutosFin = horaFin.substring(2);
                        System.out.print(horafin);
                        
                        txtHoraInicio.setText(horaInicio);
                        txtMinutoInicio.setText(minutosInicio);
                        
                        txtHoraFin.setText(horaFin);
                        txtMinutoFin.setText(miutosFin);




          //desactivarFormulario();

                    } catch (RemoteException ex) {
                        Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SQLException ex) {
                        Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
                    }




         }
      }
     } );


       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtPais = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDeleg = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtCodPostal = new javax.swing.JTextField();
        txtColonia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCalle = new javax.swing.JTextField();
        txtNumero = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        txtHoraInicio = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtMinutoInicio = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtHoraFin = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        txtMinutoFin = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        comEst = new javax.swing.JComboBox();

        setLayout(new java.awt.BorderLayout());
        add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jLabel8.setText("Pais :");

        jLabel7.setText("Estado :");

        jLabel6.setText("Delegacion :");

        jLabel5.setText("Codigo Postal :");

        txtCodPostal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodPostalActionPerformed(evt);
            }
        });

        jLabel4.setText("Colonia :");

        jLabel2.setText("Calle :");

        jLabel3.setText("Numero :");

        jLabel1.setText("Id :");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel9.setText("Administrador de Sucursal");

        jTable1.setModel(modeloTable);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        jLabel10.setText("Buscar Sucursal por ID");

        jButton4.setText("Consulta");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel11.setText("Horario");

        txtHoraInicio.setColumns(2);

        jLabel12.setText(":");

        txtMinutoInicio.setColumns(2);

        jLabel13.setText("a");

        jLabel14.setText(":");

        txtMinutoFin.setColumns(2);

        jButton1.setText("Registrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Consultar Inf");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton5.setText("Guardar Cambios");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        comEst.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Aguascalientes",
            "Baja California",
            "Baja California Sur",
            "Campeche",
            "Chiapas",
            "Chihuahua",
            "Coahuila",
            "Colima",
            "Distrito Federal",
            "Durango",
            "Guanajuato",
            "Guerrero",
            "Hidalgo",
            "Jalisco",
            "México",
            "Michoacán",
            "Morelos",
            "Nayarit",
            "Nuevo León",
            "Oaxaca",
            "Puebla",
            "Querétaro",
            "Quintana Roo",
            "San Luis Potosí",
            "Sinaloa",
            "Sonora",
            "Tabasco",
            "Tamaulipas",
            "Tlaxcala",
            "Veracruz",
            "Yucatán",
            "Zacatecas"}));

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
        .addContainerGap(387, Short.MAX_VALUE)
        .addComponent(jLabel9)
        .addGap(221, 221, 221))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
        .addGap(26, 26, 26)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addComponent(jLabel5)
            .addComponent(jLabel3)
            .addComponent(jLabel2)
            .addComponent(jLabel4)
            .addComponent(jLabel6)
            .addComponent(jLabel7)
            .addComponent(jLabel8)
            .addComponent(jLabel11))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(comEst, 0, 233, Short.MAX_VALUE)
            .addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtCalle, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtColonia, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtPais, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtDeleg, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtCodPostal, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(txtHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtMinutoInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtHoraFin, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtMinutoFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86)))
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(35, 35, 35)
                        .addComponent(jButton2)
                        .addGap(32, 32, 32)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addComponent(jButton5)
                        .addGap(109, 109, 109))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(140, 140, 140))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 484, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel9))
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(39, 39, 39)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton4)
                        .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10))))
            .addGap(35, 35, 35)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtColonia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtCodPostal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtDeleg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(comEst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8))
                    .addGap(12, 12, 12)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(txtHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel12)
                        .addComponent(txtMinutoInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel13)
                        .addComponent(txtHoraFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel14)
                        .addComponent(txtMinutoFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton5))))
            .addContainerGap(17, Short.MAX_VALUE))
    );

    add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodPostalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodPostalActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodPostalActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            try {
                calle = EValidacion.cadenaCorrecta(txtCalle.getText(), "calle");
                numeroInt = Integer.toString(EValidacion.numeroCorrecto(txtNumero.getText(), "numero Interior"));
                numeroExt = EValidacion.numeroCorrecto(txtNumero.getText(), "numero Exterior");
                colonia = EValidacion.cadenaCorrecta(txtColonia.getText(),"colonia");
                codPostal = EValidacion.numeroCorrecto(txtCodPostal.getText(), "Codigo Postal");
                dele = EValidacion.cadenaCorrecta(txtDeleg.getText(),"Delegacion");
                estado = EValidacion.cadenaCorrecta((String)comEst.getSelectedItem(), "Estado");
                direccion2 = new Direccion(calle, numeroInt, numeroExt, colonia, codPostal, delegacion, estado, "Mexico");
                openHorario = txtHoraInicio.getText() + ":" + txtMinutoInicio.getText() + ":00";
                closeHorario = txtHoraFin.getText() + ":" + txtMinutoFin.getText() + ":00";
                sucursal = new Sucursal(0, direccion2, openHorario, closeHorario);
                horaIn = EValidacion.numeroCorrecto(txtHoraInicio.getText(), "Hora");
                horaFin = EValidacion.numeroCorrecto(txtMinutoInicio.getText(), "Hora");
                int horaUno = EValidacion.numeroCorrecto(txtHoraFin.getText(), "Hora");
                int horaDos = EValidacion.numeroCorrecto(txtMinutoFin.getText(), "Hora");
                int ultSuc = servSuc2.ultimoUsuario() + 1;
                //System.out.println(ultSuc);
                direccionSucu = new Direccion(calle, "" + numeroInt, numeroExt, colonia, codPostal, dele, estado, "México");
                //int id, Direccion dir, String open, String close
                sucu = new Sucursal(ultSuc, direccionSucu, openHorario, closeHorario);
                servSuc2.altaSucursal(sucu);

            txtId.setText(servSuc2.ultimoUsuario() + 1 + "");
            txtCalle.setText("");
            txtCodPostal.setText("");
            txtColonia.setText("");
            txtDeleg.setText("");
            comEst.setSelectedIndex(0);
            //txtEstado.setText("");
            txtNumero.setText("");
            txtPais.setText("");
            txtNumero.setText("");
            txtHoraInicio.setText("");
            txtMinutoInicio.setText("");
            txtHoraFin.setText("");
            txtMinutoFin.setText("");
                System.out.println("alta exitosa");
            } catch (RemoteException ex) {
                Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExcepcionValidacion e) {
                JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
            jTable1.setModel(new ModeloTablaSucursal(servSuc2.consultaGeneral()));
          } catch (RemoteException ex) {
            Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       try{
        elimSucu = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        int opc = JOptionPane.showConfirmDialog(null, "Quiere continuar con el proceso");
           if(opc == 0){
               servSuc2.bajaSucursal(Integer.parseInt(elimSucu));
               jTable1.setModel(new ModeloTablaSucursal(servSuc2.consultaGeneral()));
               txtId.setText(servSuc2.ultimoUsuario()+1+"");
           }

       }catch(Exception e){
           JOptionPane.showMessageDialog(null, "Debe seleccionar un registro");
       }
        /*    if(txtId.getText()==""){
           JOptionPane.showMessageDialog(this,"Nohas elegido una Sucursal","ERROR",JOptionPane.ERROR_MESSAGE);
        }
       else{
            try {
                int idSucursal = EValidacion.numeroCorrecto(txtId.getText(), "ID");
                try {
                     int respond = JOptionPane.showConfirmDialog(this, "¿Seguro que desea Borrar la Sucursal?", "Alerta",JOptionPane.YES_NO_OPTION, JOptionPane.CANCEL_OPTION);
                     if(respond==JOptionPane.YES_OPTION){
                            servSuc2.bajaSucursal(idSucursal);
                                txtCodPostal.setText("");
                                txtColonia.setText("");
                                txtDeleg.setText("");
                                txtEstado.setText("");
                                txtNumero.setText("");
                                txtPais.setText("");
                                txtNumero.setText("");
                             
                          }

                } catch (RemoteException ex) {
                    Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (ExcepcionValidacion ex) {
                Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
  */
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try{
        elimSucu = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        
        
        Sucursal su = servSuc2.consultaSucursal(Integer.parseInt(elimSucu));
        Direccion dir = su.getDir();
        
        txtId.setText(su.getId()+"");
        txtCalle.setText(dir.getCalle());
        txtNumero.setText(dir.getNumExt()+"");
        txtColonia.setText(dir.getColonia());
        txtCodPostal.setText(dir.getCodPostal()+"");
        txtDeleg.setText(dir.getDelegacion());
        int t = obtenerIndex(dir.getEstado());
        comEst.setSelectedIndex(t);
        txtPais.setText(dir.getPais());
        txtHoraInicio.setText("00");
        txtMinutoInicio.setText("00");
        txtHoraFin.setText("00");
        txtMinutoFin.setText("00");

        jButton5.setEnabled(true);

        jButton1.setEnabled(false);

        }catch(Exception e){
        JOptionPane.showMessageDialog(null,"Debe seleccionar un un registro");
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        try {
                calle = EValidacion.cadenaCorrecta(txtCalle.getText(), "calle");
                numeroInt = Integer.toString(EValidacion.numeroCorrecto(txtNumero.getText(), "numero Interior"));
                numeroExt = EValidacion.numeroCorrecto(txtNumero.getText(), "numero Exterior");
                colonia = EValidacion.cadenaCorrecta(txtColonia.getText(),"colonia");
                codPostal = EValidacion.numeroCorrecto(txtCodPostal.getText(), "Codigo Postal");
                //delegacion = EValidacion.cadenaCorrecta(txtDeleg.getText(),"Delegacion");
                estado = EValidacion.cadenaCorrecta((String)comEst.getSelectedItem(), "Estado");
                dele =  EValidacion.cadenaCorrecta(txtDeleg.getText(),"delegacion");
                direccion2 = new Direccion(calle, numeroInt, numeroExt, colonia, codPostal, dele, estado, "Mexico");
                openHorario = txtHoraInicio.getText() + ":" + txtMinutoInicio.getText() + ":00";
                closeHorario = txtHoraFin.getText() + ":" + txtMinutoFin.getText() + ":00";
                sucursal = new Sucursal(0, direccion2, openHorario, closeHorario);
                horaIn = EValidacion.numeroCorrecto(txtHoraInicio.getText(), "Hora");
                horaFin = EValidacion.numeroCorrecto(txtMinutoInicio.getText(), "Hora");
                int horaUno = EValidacion.numeroCorrecto(txtHoraFin.getText(), "Hora");
                int horaDos = EValidacion.numeroCorrecto(txtMinutoFin.getText(), "Hora");
                int ultSuc = servSuc2.ultimoUsuario() + 1;
                //System.out.println(ultSuc);
                direccionSucu = new Direccion(calle, "" + numeroInt, numeroExt, colonia, codPostal, dele, estado, "México");
                //int id, Direccion dir, String open, String close
                sucu = new Sucursal(Integer.parseInt(elimSucu), direccionSucu, openHorario, closeHorario);
                servSuc2.cambioSucursal(sucu);

                jTable1.setModel(new ModeloTablaSucursal(servSuc2.consultaGeneral()));

                 txtCalle.setText("");
            txtCodPostal.setText("");
            txtColonia.setText("");
            txtDeleg.setText("");
            comEst.setSelectedIndex(0);
            txtNumero.setText("");
            txtPais.setText("");
            txtNumero.setText("");
            txtHoraInicio.setText("");
            txtMinutoInicio.setText("");
            txtHoraFin.setText("");
            txtMinutoFin.setText("");

            jButton5.setEnabled(false);

            jButton1.setEnabled(true);

                txtId.setText(servSuc2.ultimoUsuario()+1+"");
            } catch (RemoteException ex) {
                Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(AdministradorAdministrarSucursal.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "verifique el codigo postal");

            } catch (ExcepcionValidacion e) {
                JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

    }//GEN-LAST:event_jButton4ActionPerformed


    private void activarFormulario(){
       // txtCalle.setEditable(true);
        //txtCodPostal.setEditable(true);
        //txtColonia.setEditable(true);
        //txtDeleg.setEditable(true);
        //txtEstado.setEditable(true);
        //txtNumero.setEditable(true);
        //txtPais.setEditable(true);
        //txtnum2.setEditable(true);
       
 
    }

    private void desactivarFormulario(){
       // txtCodPostal.setEditable(false);
        //txtColonia.setEditable(false);
        //txtDeleg.setEditable(false);
        //txtEstado.setEditable(false);
        //txtNumero.setEditable(false);
        //txtPais.setEditable(false);
        //txtnum2.setEditable(false);
  
        //txtCalle.setEditable(false);
        //txtId.setEditable(false);

    }

    public int obtenerIndex(String estado){
       
        int index = 0;
        int a = 0;
        
       if(estado.equals("Aguascalientes")){index = 0+a;
       }
       if(estado.equals("Baja California")){index = 1+a;
       }
       if(estado.equals("Baja California Sur")){index = 2+a;
       }
       if(estado.equals("Campeche")){index = 3+a;
       }
       if(estado.equals("Chiapas")){index = 4+a;
       }
       if(estado.equals("Chihuahua")){index = 5+a;
       }
       if(estado.equals("Coahuila")){index = 6+a;
       }
       if(estado.equals("Colima")){index = 7+a;
       }
       if(estado.equals("Distrito Federal")){index = 8+a;
       }
       if(estado.equals("Durango")){index = 9+a;
       }
       if(estado.equals("Guanajuato")){index = 10+a;
       }
       if(estado.equals("Guerrero")){index = 11+a;
       }
       if(estado.equals("Hidalgo")){index = 12+a;
       }
       if(estado.equals("Jalisco")){index = 13+a;
       }
       if(estado.equals("México")){index = 14+a;
       }
       if(estado.equals("Michoacán")){index = 15+a;
       }
       if(estado.equals("Morelos")){index = 16+a;
       }
       if(estado.equals("Nayarit")){index = 17+a;
       }
       if(estado.equals("Nuevo León")){index = 18+a;
       }
       if(estado.equals("Oaxaca")){index = 19+a;
       }
       if(estado.equals("Puebla")){index = 20+a;
       }
       if(estado.equals("Querétaro")){index = 21+a;
       }
       if(estado.equals("Quintana Roo")){index = 22+a;
       }
       if(estado.equals("San Luis Potosí")){index = 23+a;
       }
       if(estado.equals("Sinaloa")){index = 24+a;
       }
       if(estado.equals("Sonora")){index = 25+a;
       }
       if(estado.equals("Tabasco")){index = 26+a;
       }
       if(estado.equals("Tamaulipas")){index = 27+a;
       }
       if(estado.equals("Tlaxcala")){index = 28+a;
       }
       if(estado.equals("Veracruz")){index = 29+a;
       }
       if(estado.equals("Yucatán")){index = 30+a;
       }
       if(estado.equals("Zacatecas")){index = 31+a;
       }
       
        
       return index;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comEst;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JTextField txtCalle;
    private javax.swing.JTextField txtCodPostal;
    private javax.swing.JTextField txtColonia;
    private javax.swing.JTextField txtDeleg;
    private javax.swing.JTextField txtHoraFin;
    private javax.swing.JTextField txtHoraInicio;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtMinutoFin;
    private javax.swing.JTextField txtMinutoInicio;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtPais;
    // End of variables declaration//GEN-END:variables

}
